package com.xhej.rowreat.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class CommonUtils {
	
	/**
	 * 分组依据接口，用于集合分组时，获取分组依据
	 * @author ZhangDanDan
	 *
	 * @param <Integer>
	 */
	public interface GroupBy<Integer> {  

		Integer GroupBy(Object obj);  
    }
	
	public static final <T extends Comparable<T>, D> Map<T, List<D>> group(Collection<D> colls, GroupBy<T> gb) {  
        if (colls == null || colls.isEmpty()) {  
            System.out.println("分组集合不能为空!");  
            return null;  
        }  
        if (gb == null) {  
            System.out.println("分组依据接口不能为Null!");  
            return null;  
        }  
        Iterator<D> iter = colls.iterator();  
        Map<T, List<D>> map = new HashMap<T, List<D>>();  
        while (iter.hasNext()) {  
            D d = iter.next();  
            T t = gb.GroupBy(d);  
            if (map.containsKey(t)) {  
                map.get(t).add(d);  
            } else {  
                List<D> list = new ArrayList<D>();  
                list.add(d);  
                map.put(t, list);  
            }  
        }  
        return map;  
    }  
}
